package com.briup.pts.service.impl;

import com.briup.pts.config.exception.ServiceException;
import com.briup.pts.config.response.ResultCode;
import com.briup.pts.domain.bean.Animal;
import com.briup.pts.domain.bean.Hurdles;
import com.briup.pts.domain.bean.IndexRecord;
import com.briup.pts.domain.vo.BatchVO;
import com.briup.pts.domain.vo.DiseaseRecordVO;
import com.briup.pts.domain.vo.SourceVO;
import com.briup.pts.mapper.*;
import com.briup.pts.service.SourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SourceServiceImpl implements SourceService {

    private final AnimalMapper animalMapper;
    private final BatchMapper batchMapper;
    private final HurdlesMapper hurdlesMapper;
    private final DiseaseRecordMapper diseaseRecordMapper;
    private final IndexRecordMapper indexRecordMapper;

    @Override
    public SourceVO queryByAnimalId(String animalId) {
        // 根据动物id查询动物信息
        Animal animal = animalMapper.queryByIdWithStatus(animalId, null);
        if (animal==null){
            throw new ServiceException(ResultCode.ANIMAL_IS_DELETED);
        }
        // 根据id查询栏圈信息
        Hurdles hurdles = hurdlesMapper.queryById(animal.getAHurdlesId());
        // 查询批次的信息
        BatchVO batch = batchMapper.queryByIdWithQR(animal.getABatchId());
        // 根据动物id查询对应的病症记录
        List<DiseaseRecordVO> recordVOS = diseaseRecordMapper.queryByAnimalId(animalId);
        // 根据动物id查询对应的指标变化记录
        List<IndexRecord> indexRecords = indexRecordMapper.queryByAnimalId(animalId);

        // 构建SourceVO对象，并返回
        return SourceVO.builder()
                .animal(animal)
                .hurdles(hurdles)
                .batch(batch)
                .diseaseRecords(recordVOS)
                .indexRecords(indexRecords)
                .build();
    }
}
